<!-- 商品分类 -->
<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col :span="3">
          <el-button type="primary" @click="showAddCate">添加分类</el-button>
        </el-col>
      </el-row>
      <tree-table
        class="tree-table"
        :data="cateData"
        :columns="columns"
        border
        :show-row-hover="false"
        :selection-type="false"
        :expand-type="false"
        show-index
        index-text="#"
      >
        <template slot="isok" slot-scope="scope">
          <i class="el-icon-success" v-if="scope.row.cat_deleted === '1'" style="color: green"></i>
          <i class="el-icon-error" v-else style="color: red"></i>
        </template>
        <template slot="order" slot-scope="scope">
          <el-tag type="primary" size="min" v-if="scope.row.v === 1">一级</el-tag>
          <el-tag type="success" size="min" v-if="scope.row.v === 2">二级</el-tag>
          <el-tag type="warning" size="min" v-if="scope.row.v === 3">三级</el-tag>
        </template>
        <template slot="opt" slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" @click="editCate(scope.row)" size="mini">编辑</el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            @click="deleteCate(scope.row)"
            size="mini"
          >删除</el-button>
        </template>
      </tree-table>
    </el-card>
    <el-dialog
      :title="addOrEidtCate === 'add' ? '添加分类' : '编辑分类'"
      :visible.sync="addCatedialogVisible"
      @close="closeCateDialog"
      width="50%"
    >
      <el-form :model="addCateForm" :rules="addCateRules" ref="addCateRef" label-width="100px">
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="addCateForm.name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类" v-if="addOrEidtCate === 'add'">
          <el-cascader
            v-model="cascaderValue"
            :options="parentCateData"
            :props="cateProps"
            @change="cascaderChange"
            clearable
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCatedialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { reqParentCateList, reqAddCate, reqDeleteCate, reqUpdateCate } from '@/api'
export default {
  data() {
    return {
      cateData: [],
      parentCateData: [],
      cateParams: {
        cid: '3'
      },
      columns: [
        {
          label: '分类名称',
          prop: 'name',
          width: '400px'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isok'
        },
        {
          label: '排序',
          type: 'template',
          template: 'order'
        },
        {
          label: '操作',
          type: 'template',
          template: 'opt',
          width: '200px'
        }
      ],
      addCatedialogVisible: false,
      addCateForm: {
        cid: '', // 当前分类1 2 3 三级
        parentId: '', // 父级分类，0为一级
        name: '', // 分类名称
        v: 1 // 等级
      },
      editCateForm: {
        id: '',
        cid: '',
        name: ''
      },
      addCateRules: {
        name: [{ required: true, message: '请输入分类名称', trigger: 'blur' }]
      },
      cascaderValue: [],
      cateProps: {
        expandTrigger: 'hover',
        label: 'name',
        value: 'id',
        checkStrictly: true
      },
      addOrEidtCate: ''
    }
  },

  created() {
    this.getCateList()
  },

  methods: {
    // 获取所有分类信息
    async getCateList() {
      const result = await reqParentCateList(3)
      if (result.code !== 200) {
        this.$message.error('获取分类列表失败！')
      }
      this.cateData = result.data
      // console.log(res.data)
    },

    // 获取添加分类信息
    async getParentCateList() {
      const res = await reqParentCateList(2)
      if (res.code !== 200) {
        this.$message.error('获取分类列表失败！')
      }
      this.parentCateData = res.data
    },

    // 弹出添加分类对话框
    showAddCate() {
      this.addOrEidtCate = 'add'
      this.getParentCateList()
      // 设置默认值
      this.addCateForm.parentId = '0'
      this.addCateForm.cid = '1'
      this.addCateForm.v = 1
      this.addCatedialogVisible = true
    },

    // 关闭添加分类对话框
    closeCateDialog() {
      this.$refs.addCateRef.resetFields()
      this.cascaderValue = []
    },

    // 确定添加分类
    async addCate() {
      this.$refs.addCateRef.validate(async valid => {
        if (!valid) return
        let result
        if (this.addOrEidtCate === 'add') {
          result = await reqAddCate(this.addCateForm)
        } else {
          this.editCateForm.name = this.addCateForm.name
          result = await reqUpdateCate(this.editCateForm)
        }
        if (result.code === 201) {
          this.$message.success(
            `${this.addOrEidtCate === 'add' ? '添加' : '修改'}分类成功！`
          )
          this.getCateList()
          // 提高用户体验，目前只做了更新功能
          // this.cateData = result.data
        } else {
          this.$message.error(
            `${this.addOrEidtCate === 'add' ? '添加' : '修改'}分类失败！`
          )
        }
        this.addCatedialogVisible = false
      })
    },

    // 编辑分类
    editCate(row) {
      this.addCatedialogVisible = true
      this.addOrEidtCate = 'edit'
      this.addCateForm.name = row.name
      this.editCateForm.id = row.id
      this.editCateForm.cid = row.cid
    },

    // 删除分类
    deleteCate(row) {
      this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      .then(async () => {
        const id = row.id
        const cid = row.cid
        const result = await reqDeleteCate(id, cid)
        if (result.code === 204) {
          this.$message.success('删除成功！')
          this.getCateList()
        } else {
          this.$message.error('删除失败！')
        }
        this.addCatedialogVisible = false
      })
      .catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },

    // 级联选择
    cascaderChange() {
      // console.log(this.cascaderValue)
      const keys = this.cascaderValue
      // 选中父级分类
      if (keys.length > 0) {
        this.addCateForm.parentId = keys[keys.length - 1]
        this.addCateForm.cid = keys.length + 1
        this.addCateForm.v = keys.length + 1
      } else {
        // 没有父级，说明是第一级
        this.addCateForm.parentId = '0'
        this.addCateForm.cid = '1'
        this.addCateForm.v = 1
      }
    }
  }
}
</script>
<style lang='less' scoped>
.tree-table {
  margin-top: 15px;
}
</style>
